package com.liuxingzhiwu.ruijishangcheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuxingzhiwu.ruijishangcheng.dto.DishDto;
import com.liuxingzhiwu.ruijishangcheng.entity.Dish;
import com.liuxingzhiwu.ruijishangcheng.entity.DishFlavor;
import com.liuxingzhiwu.ruijishangcheng.mapper.DishMapper;
import com.liuxingzhiwu.ruijishangcheng.service.DishFlavorService;
import com.liuxingzhiwu.ruijishangcheng.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void saveWithFlavor(HttpServletRequest request, DishDto dishDto) {
        Long empId = (Long) request.getSession().getAttribute("employee");

        //1、在菜品表里面添加菜品
        dishDto.setCreateTime(LocalDateTime.now());
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        this.save(dishDto);

        //2、添加菜品口味（口味有多中是一个List数组）
        List<DishFlavor> flavors = dishDto.getFlavors();
        //在菜品口味里面添加菜品id
        //将集合里面的元素取出来添加id等数据，在返回集合
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            item.setCreateUser(empId);
            item.setUpdateUser(empId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public void updateWithFlavor(HttpServletRequest request, DishDto dishDto) {

        Long empId = (Long) request.getSession().getAttribute("employee");

        //1、在菜品表里面更新菜品
        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setUpdateUser(empId);

        //根据菜品id更新菜品
        this.updateById(dishDto);

        //2、更新菜品口味（口味有多中是一个List数组）
        List<DishFlavor> flavors = dishDto.getFlavors();

        //删除原来的口味，添加新的口味
        QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dish_id",dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //在菜品口味里面添加菜品id
        //将集合里面的元素取出来添加id等数据，在返回集合
        flavors = flavors.stream().map((item)->{
            item.setId(null); //重新生成口味id
            item.setDishId(dishDto.getId());
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            item.setCreateUser(empId);
            item.setUpdateUser(empId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

}
